package com.xmkj.winmoney.home;

import android.os.Handler;
import android.os.Message;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;

import com.common.Entity.FenLeiListEntity;
import com.common.Entity.SortEntity;
import com.common.cons.Constans;
import com.common.mvp.BaseMvpActivity;
import com.common.mvp.BasePresenter;
import com.common.retrofit.method.FenLeiListMethods;
import com.common.retrofit.subscriber.CommonSubscriber;
import com.common.retrofit.subscriber.SubscriberListener;
import com.common.utils.EmptyUtils;
import com.common.utils.MyItemAnimator;
import com.common.utils.SPUtils;
import com.common.widget.recyclerview.refresh.recycleview.XRecyclerView;
import com.xmkj.winmoney.R;

import java.util.ArrayList;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

public class FenLeiListActivity extends BaseMvpActivity {
    public static final String BEAN = "beans";
    private SortEntity.ListsEntity sortBean;
    XRecyclerView recyclerview;
    private FenLeiAdapter adapter;
    List<FenLeiListEntity.ListsBean> bean =   new ArrayList<>() ;
    private Timer timer;

    @Override
    protected BasePresenter createPresenterInstance() {
        return null;
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_base_reclerview;
    }

    @Override
    protected void onViewCreated() {
        recyclerview = findViewById(R.id.recyclerview);

    }
    private Handler mHandler = new Handler(){
        @Override
        public void handleMessage(Message msg) {

            // TODO Auto-generated method stub
            if(msg.what == 0){
                //这里可以进行UI操作，如Toast，Dialog等
                System.out.println("hhaha");
                gotoHttpRep();
            }
        }
    };
    @Override
    protected void doLogicFunc() {
        if(EmptyUtils.isNotEmpty(sortBean)){
            gotoHttpRep();
            setRecyclerView();

          startTimer();
        }

    }
    TimerTask  mTimerTask ;
    // 开启时间
    public void startTimer() {
        if (timer == null) {
            timer = new Timer();
        }
        if(mTimerTask!=null){
            mTimerTask.cancel();
            mTimerTask = null;
        }
        if(mTimerTask==null){
            mTimerTask  =  new TimerTask() {

                @Override
                public void run() {
                    // TODO Auto-generated method stub

                    Message message = new Message();
                    message.what = 0;
                    mHandler.sendMessage(message);
                }
            };
        }
        timer.schedule(mTimerTask, 500, 2000);
    }

    private void stopTimer() {
        if (timer != null) {
            timer.cancel();
            timer = null;
        }

        if (mTimerTask != null) {
            mTimerTask.cancel();
            mTimerTask = null;
        }
    }
    @Override
    public void onDestroy() {
        stopTimer();
        super.onDestroy();
    }
    @Override
    public void onPause() {
        stopTimer();
        super.onPause();
    }
    @Override
    public void onResume() {
        startTimer();
        super.onResume();
    }
    private void setRecyclerView() {
//        IRecyclerViewHelper.init().setRecycleLineLayout(context, LinearLayoutManager.VERTICAL, recyclerview);
        GridLayoutManager layoutManager = new GridLayoutManager(context, 2);
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        recyclerview.setLayoutManager(layoutManager);
//        recyclerview.addItemDecoration(new DividerGridItemDecoration(context));
      /*  recyclerview.addItemDecoration(new MarginDecoration(LinearLayoutManager.VERTICAL, (int) SizeUtils.dp2px(context, 1)));
        recyclerview.addItemDecoration(new MarginDecoration(LinearLayoutManager.HORIZONTAL, (int) SizeUtils.dp2px(context, 1)));*/
//        recyclerview.addItemDecoration(new MarginDecoration(LinearLayoutManager.VERTICAL, (int) SizeUtils.dp2px(context, 1)));
        recyclerview.setHasFixedSize(true);
        recyclerview.setRefreshing(true);
        recyclerview.setLoadMoreEnabled(true);
        adapter = new FenLeiAdapter(context, bean);
//        adapter.setOnItemClickSeclect(this);
        recyclerview.setAdapter(adapter);
        recyclerview.setItemAnimator(new MyItemAnimator());
        recyclerview.setLoadingListener(new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {
                mPageIndex = 1;
                mIsRefreshOrLoadMore = 0;
                gotoHttpRep();
            }

            @Override
            public void onLoadMore() {
                if (mIsHasNoData) {
                    recyclerview.loadMoreComplete();
                    recyclerview.setNoMore(true);
                    return;
                }
                mPageIndex++;
                mIsRefreshOrLoadMore = 1;
                gotoHttpRep();
            }
        });
    }



    private void gotoHttpRep() {
        CommonSubscriber<FenLeiListEntity> subscriber = new CommonSubscriber<>(new SubscriberListener() {
            @Override
            public void onNext(Object o) {
                dismissProgressDialog();
                FenLeiListEntity beans = (FenLeiListEntity) o;
                List<FenLeiListEntity.ListsBean> beanList = beans.lists;
                recyclerview.loadMoreComplete();
                // 下拉刷新
                if (mIsRefreshOrLoadMore == 0) {
                    recyclerview.refreshComplete();
                    adapter.clear();
                }

                if (EmptyUtils.isNotEmpty(beanList)) {
                    bean = beanList;
                    adapter.addAll(bean);
                } else if (mIsRefreshOrLoadMore == 0) {
//                    statusEmpty();
                    return;
                }

                if (EmptyUtils.isEmpty(beanList)) {
                    recyclerview.setNoMore(true);
                } else {
                    mIsHasNoData = beanList.size() < mPageSize;
                    recyclerview.setNoMore(mIsHasNoData);
                }
                adapter.notifyDataSetChanged();
                recyclerview.refreshComplete();
                recyclerview.loadMoreComplete();
                if (mIsRefreshOrLoadMore == 0) {
                    dismissProgressDialog();
                }
            }

            @Override
            public void onError(String e, int code) {
                dismissProgressDialog();
                adapter.notifyDataSetChanged();
                recyclerview.refreshComplete();
                recyclerview.loadMoreComplete();
                if (mIsRefreshOrLoadMore == 0) {
                    dismissProgressDialog();
                }
            }
        });


        FenLeiListMethods.getInstance().seeGood(subscriber, SPUtils.getShareInt(Constans.UID), SPUtils.getShareString(Constans.HASHID), mPageIndex,sortBean.cid+"");
        rxManager.add(subscriber);
    }
    @Override
    protected void setNavigation() {
        sortBean = (SortEntity.ListsEntity) getIntent().getSerializableExtra(BEAN);
        if(EmptyUtils.isNotEmpty(sortBean)){
            setNavigationBack(sortBean.ctitle);
        }
    }
}
